package org.tio.sitexxx.service.service.tiosite;

import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.sitexxx.service.cache.CacheConfig;
import org.tio.sitexxx.service.cache.Caches;
import org.tio.sitexxx.service.init.JFInit;
import org.tio.sitexxx.service.model.main.Case;
import org.tio.utils.cache.ICache;

import com.jfinal.json.MixedJsonFactory;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;

/**
 * @author tanyaowu
 * 2016年7月22日 上午10:44:55
 */
public class BsService {
	@SuppressWarnings("unused")
	private static Logger			log	= LoggerFactory.getLogger(BsService.class);
	public static final BsService	me	= new BsService();

	/**
	 * @param args
	 * @author tanyaowu
	 * @throws SQLException 
	 */
	public static void main(String[] args) throws Exception {
		JFInit.init();

		Page<Record> page = BsService.me.page(1, 10);

		System.out.println(MixedJsonFactory.me().getJson().toJson(page));
	}

	private final Case dao = new Case().dao();

	/**
	 *
	 * @author tanyaowu
	 */
	public BsService() {
	}

	@SuppressWarnings("unchecked")
	public Page<Record> page(int pageNumber, int pageSize) {
		String cacheKey = "bs_page" + "_" + pageNumber + "_" + pageSize;

		ICache cache = Caches.getCache(CacheConfig.TIME_TO_LIVE_SECONDS_5);
		Object obj = cache.get(cacheKey);
		if (obj != null) {
			return (Page<Record>) obj;
		}

		SqlPara sqlPara = dao.getSqlPara("bs.page");
		Page<Record> ret = Db.paginate(pageNumber, pageSize, sqlPara);//.p.paginate(pageNumber, pageSize, sqlPara);
		cache.put(cacheKey, ret);
		return ret;
	}
}
